package com.ichart.doc;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用于生成文档、抽取出Component的信息、并封装成Component对象
 * create at 2012-4-27 下午03:17:03
 * @author wanghe
 */
public class ComponentResolver{
	/**
	 * Pattern for extract Component
	 */
	//public static Pattern COMPONENT = Pattern.compile("@overview(.*?)@component#([\\w@#]*\\.\\w*|[\\w@#]*).*?@extend#(\\w*\\.\\w*|\\w*).*?this\\.set\\(\\{(.*?)\\}\\).*?this\\.registerEvent\\((.*?)\\)", Pattern.DOTALL);
	public static Pattern COMPONENT = Pattern.compile("@overview(.*?)@component#([\\w@#]*\\.\\w*|[\\w@#]*).*?@extend#(\\w*\\.\\w*|\\w*)(.*?)@end", Pattern.DOTALL);
	public static Pattern CONFIGURATION = Pattern.compile("\\.set\\(\\{(.*?)\\}\\)", Pattern.DOTALL);
	public static Pattern REGISTEREVENT = Pattern.compile("\\.registerEvent\\((.*?)\\)", Pattern.DOTALL);
	public static Pattern METHOD = Pattern.compile("@method(.*?)\\*/.*?(\\w*)\\s*:\\s*function", Pattern.DOTALL);
	public static Pattern SPLITROW = Pattern.compile("^(.*)$",Pattern.MULTILINE);
	
	//TODO Home编写
	
	private Component buildComponent(Matcher matcher){
		Component component = new Component();
		component.setOverview(SPLITROW.matcher(matcher.group(1).trim().replaceAll("\\*","")).replaceAll("$1<br/>"));
		component.setName(matcher.group(2));
		component.setExtend(matcher.group(3));
		
		Matcher m1 = CONFIGURATION.matcher(matcher.group(4));
		while(m1.find()){  
			component.config(m1.group(1));
        }
		
		Matcher m2 = REGISTEREVENT.matcher(matcher.group(4));
		while(m2.find()){  
			component.registerEvent(m2.group(1));
        }
		
		Matcher m3 = METHOD.matcher(matcher.group(4));
		while(m3.find()){  
			component.registerMethod(m3.group(2),m3.group(1));
        }
		
		return component;
	}
	
	public List<Component> resolver(String content){
		List<Component> list = new ArrayList<Component>();
		Matcher matcher = COMPONENT.matcher(content);
		while(matcher.find()){  
			list.add(buildComponent(matcher));
        }
		return list;
	}
	
	
}	
